package com.noahplat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.*;
import com.noahplat.user.pojo.*;
import com.noahplat.user.service.MenuService;
import com.noahplat.user.service.PermissionService;
import com.noahplat.user.utils.MenuDao;
import com.noahplat.user.utils.PermissionAdminDao;
import com.noahplat.user.utils.PermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * 菜单栏相关接口
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/2/27 13:28
 **/
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 分页获取权限
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getPermission(Long stid,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Permission> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(stid)){
            list = permissionMapper.selectPermission();
        }else {
            list = permissionMapper.selectPermissionByStid(stid);
        }
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Permission> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @Override
    public Result addPermission(Permission permission){
        int res = permissionMapper.insertSelective(permission);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @Override
    public Result updatePermission(Permission permission){
        int res = permissionMapper.updateByPrimaryKeySelective(permission);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @Override
    public Result delPermission(Long id){
        Example example = new Example(RolePermission.class);
        example.createCriteria().andEqualTo("pid",id);
        List<RolePermission> list = rolePermissionMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"禁止删除,正在使用");
        }
        int res = permissionMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 添加权限
     * @param permissionDao
     * @return
     */
    @Override
    public Result inserPermission(PermissionDao permissionDao){
        User user = userMapper.selectByPrimaryKey(permissionDao.getUid());
        if(ObjectUtils.isEmpty(user)){
            return new Result(false,StatusCode.ERROR,"操作人信息出错");
        }
        List<Permission> list = new ArrayList<>();
        for(Long acctid : permissionDao.getAcctid()){
            for(Permission permission : permissionDao.getList()){
                Permission permission1 = new Permission();
                permission1.setStid(permissionDao.getStid());
                permission1.setAcctid(acctid);
                permission1.setPermission(permission.getPermission());
                permission1.setDescription(permission.getDescription());
                permission1.setCreatedBy(user.getName());
                permission1.setCreatedTime(new Date());
                list.add(permission1);
            }
        }
        int res = permissionMapper.insertPermission(list);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 查询权限信息
     * @return
     */
    @Override
    public Result getPermissions() {
        List<PermissionAdminDao> list = permissionMapper.getPermission();
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.ERROR, "暂无数据");
        }

        return new Result(true, StatusCode.OK, "查询成功", list);
    }

}
